﻿using System;
using System.Collections.Generic;
using System.Text;
using DotNet.Model;
using DotNet.Common;
using DotNet.Data.Factory;
using System.Collections;
using System.Data;
using System.Data.SqlClient;

namespace DotNet.Business
{
    public class CompanyDal
    {
        IDBHelp db = DBHelp.Create();
        Hashtable htSyn = Hashtable.Synchronized(new Hashtable());
        StringBuilder strSql = new StringBuilder();
        #region 是否存在该记录
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int companyId)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("select count(1) from Company where companyId=@companyId ");
            htSyn.Add("companyId", companyId);
            return db.IsExists(strSql.ToString(), htSyn);
        }
        #endregion

        #region 获得公司信息列表
        /// <summary>
        /// 获得公司信息列表
        /// </summary>
        /// <returns></returns>
        public IList<Company> GetModel()
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("SELECT [companyId],[companyName],[Welcome],[Logo],[companyInfo],[Website],[companyImg],[Country],[City],[Province],[Zipcode],[Tel],[Fax],[companyType],[companyDate],[Trade],[people],[Turnover],companyAddress FROM [Company]");
            return getProductsBySql(strSql.ToString());
        }
        #endregion

        #region 根据公司信息号查询实体
        /// <summary>
        /// 根据公司信息号查询实体
        /// </summary>
        /// <param name="companyId"></param>
        /// <returns></returns>
        public Company GetModel(int companyId)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("SELECT [companyId],[companyName],[Welcome],[Logo],[companyInfo],[Website],[companyImg],[Country],[City],[Province],[Zipcode],[Tel],[Fax],[companyType],[companyDate],[Trade],[people],[Turnover],companyAddress FROM [Company]");
            strSql.Append(" where companyId=@companyId ");
            htSyn.Add("@companyId", companyId);
            DataTable dt = db.GetDataTable(strSql.ToString(), htSyn);
            Company company = new Company();
            if (dt.Rows.Count>0)
            {
                company.companyId = Convert.ToInt32(dt.Rows[0]["companyId"]);
                company.companyName = dt.Rows[0]["companyName"].ToString();
                company.Welcome = dt.Rows[0]["Welcome"].ToString();
                company.Logo = dt.Rows[0]["Logo"].ToString();
                company.companyInfo = dt.Rows[0]["companyInfo"].ToString();
                company.Website = dt.Rows[0]["Website"].ToString();
                company.companyImg = dt.Rows[0]["companyImg"].ToString();
                company.Country = dt.Rows[0]["Country"].ToString();
                company.City = dt.Rows[0]["City"].ToString();
                company.Province = dt.Rows[0]["Province"].ToString();
                company.Zipcode = dt.Rows[0]["Zipcode"].ToString();
                company.Tel = dt.Rows[0]["Tel"].ToString();
                company.Fax = dt.Rows[0]["Fax"].ToString();
                company.CompanyType = dt.Rows[01]["CompanyType"].ToString();
                company.CompanyDate = Convert.ToDateTime(dt.Rows[0]["CompanyDate"]);
                company.Trade = dt.Rows[0]["Trade"].ToString();
                company.People = dt.Rows[0]["People"].ToString();
                company.Turnover = dt.Rows[0]["Turnover"].ToString();
                company.CompanyAddress = dt.Rows[0]["CompanyAddress"].ToString();
            }            
            return company;
        }
        #endregion

        #region 修改公司信息信息
        /// <summary>
        /// 修改公司信息信息
        /// </summary>
        /// <param name="company"></param>
        /// <returns></returns>
        public bool Update(Company company)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("update Company set ");
            strSql.Append("companyName=@companyName,");
            strSql.Append("Welcome=@Welcome,");
            strSql.Append("Logo=@Logo,");
            strSql.Append("companyInfo=@companyInfo,");
            strSql.Append("Website=@Website,");
            strSql.Append("companyImg=@companyImg,");

            strSql.Append("Country =@Country,");
            strSql.Append("City=@City,");
            strSql.Append("Province=@Province,");
            strSql.Append("Zipcode=@Zipcode,");
            strSql.Append("Tel=@Tel,");
            strSql.Append("Fax=@Fax,");
            strSql.Append("companyType=@companyType,");
            strSql.Append("companyDate=@companyDate,");
            strSql.Append("Trade=@Trade,");
            strSql.Append("people=@people,");
            strSql.Append("Turnover=@Turnover, ");
            strSql.Append("companyAddress=@companyAddress ");

            strSql.Append(" where companyId=@companyId ");
            htSyn.Add("@companyId",company.companyId);
            htSyn.Add("@companyName",company.companyName);
            htSyn.Add("@Welcome",company.Welcome);
            htSyn.Add("@Logo",company.Logo);
            htSyn.Add("@companyInfo",company.companyInfo);
            htSyn.Add("@Website",company.Website);
            htSyn.Add("@companyImg", company.companyImg);

            htSyn.Add("@Country", company.Country);
            htSyn.Add("@City", company.City);
            htSyn.Add("@Province", company.Province);
            htSyn.Add("@Zipcode", company.Zipcode);
            htSyn.Add("@Tel", company.Tel);
            htSyn.Add("@Fax", company.Fax);
            htSyn.Add("@companyType", company.CompanyType);
            htSyn.Add("@companyDate", company.CompanyDate);
            htSyn.Add("@Trade", company.Trade);
            htSyn.Add("@people", company.People);
            htSyn.Add("@Turnover", company.Turnover);

            htSyn.Add("@companyAddress", company.CompanyAddress);
            if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region 删除公司信息根据公司信息号
        /// <summary>
        /// 删除公司信息根据公司信息号
        /// </summary>
        /// <param name="companyId"></param>
        /// <returns></returns>
        public bool Delete(int companyId)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("delete from Company where companyId=@companyId");
            htSyn.Add("companyId", companyId);
            if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region 根据SQL语句获取集合
        /// <summary>
        ///根据SQL语句获取集合
        /// </summary>
        public IList<Company> getProductsBySql(string strSql)
        {
            IList<Company> list = new List<Company>();
            DataTable dt = db.GetDataTable(strSql);
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    Company company = new Company();
                    company.companyId = Convert.ToInt32(dt.Rows[0]["companyId"]);
                    company.companyName = dr["companyName"].ToString();
                    company.Welcome = dr["Welcome"].ToString();
                    company.Logo = dr["Logo"].ToString();
                    company.companyInfo = dr["companyInfo"].ToString();
                    company.Website = dr["Website"].ToString();
                    company.companyImg = dr["companyImg"].ToString();
                    company.Country = dr["Country"].ToString();
                    company.City = dr["City"].ToString();
                    company.Province = dr["Province"].ToString();
                    company.Zipcode = dr["Zipcode"].ToString();
                    company.Tel = dr["Tel"].ToString();
                    company.Fax = dr["Fax"].ToString();
                    company.CompanyType = dr["CompanyType"].ToString();
                    company.CompanyDate = Convert.ToDateTime(dr["CompanyDate"]);
                    company.Trade = dr["Trade"].ToString();
                    company.People = dr["People"].ToString();
                    company.Turnover = dr["Turnover"].ToString();
                    company.CompanyAddress = dr["CompanyAddress"].ToString();
                    list.Add(company);
                }
            }
            return list;
        }
        #endregion
    }
}
